package com.yc.home.cloudother.controller;

import com.yc.bean.Result;
import com.yc.home.cloudother.mapper.CreateclassMapper;
import com.yc.home.cloudother.mapper.DoquestionMapper;
import com.yc.home.cloudother.mapper.ExamMapper;
import com.yc.home.cloudother.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("back")
public class backController {

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    DoquestionMapper doquestionMapper;

    @Autowired
    CreateclassMapper createclassMapper;

    @Autowired
    ExamMapper examMapper;

    @Autowired
    TypeMapper typeMapper;

    @RequestMapping("getdataL")
    Result getdataL(){
        //获取用户数量
        String url = "http://cloud-user/user/getnum";
        ResponseEntity<Result> forEntity = restTemplate.getForEntity(url, Result.class);
        // 处理user服务的响应
        int num1 = 0;
        if (forEntity.getStatusCode().is2xxSuccessful()) {
            Result userServiceResult = forEntity.getBody();
            num1 = (int) userServiceResult.getData();
        } else {
            return new Result(0, "调用用户服务失败", null);
        }
        //获取总刷题量
        int num2 = doquestionMapper.getNum();
        //获取总班级数
        int num3 = Math.toIntExact(createclassMapper.selectCount(null));
        //获取全部试卷数
        int num4 = Math.toIntExact(examMapper.selectCount(null));
        String num = String.valueOf(num1) + ":" +
                String.valueOf(num2) + ":" +
                String.valueOf(num3) + ":" +
                String.valueOf(num4);
        return new Result(1, "数量", num);
    }

    @RequestMapping("getmonth")
    Result getmonth(String year){
        List<Map<String, String>> getyear = doquestionMapper.getmonth(year);
        return new Result(1, "成功", getyear);
    }

    @RequestMapping("getyear")
    Result getyear(){
        List<Map<String, String>> getyear = doquestionMapper.getyear();
        return new Result(1, "成功", getyear);
    }

    @RequestMapping("getjd")
    Result getjd(String year){
        List<Map<String, String>> getyear = doquestionMapper.getjd(year);
        return new Result(1, "成功", getyear);
    }

    @RequestMapping("getfivetype")
    Result getfivetype(){
        List<Map<String, String>> getfivetype = typeMapper.getfivetype();
        return new Result(1, "成功", getfivetype);
    }

    @RequestMapping("getsevenproduct")
    Result getsevenproduct(){
        List<Map<String, String>> getsevenproduct = doquestionMapper.getsevenproduct();
        return new Result(1, "成功", getsevenproduct);
    }
}
